<#import "../tpl/pageTep.ftl" as page/>
<@page.pageBase currentMenu="商品目录">
<style>
    .form-horizontal .controls {
        line-height: 40px;
        height: 40px;
    }
    .button-primary,.button-success,.button-danger {
        margin-left: 5px;
    }
</style>
<form id="searchForm" class="form-panel">
    <ul class="panel-content">
        <li>
            <div class="control-group span5">
                <div class="controls">
                    <input type="text" name="title" id="title" value="" placeholder="输入关键字搜索">
                </div>
            </div>
            <div class="form-actions span8">
                <button type="submit" class="button  button-primary">
                    搜索
                </button>
                <a href="javascript:add()" class="button button-success">添加</a>
                <a class="button  button-danger" href="javascript: delFunction()">
                    删除
                </a>
            </div>
        </li>
    </ul>
</form>
<div id="t1"></div>
<#------------------------------------------------------添加表单start------------------------------------------------------->
<div id="addContent" style="display:none;">
    <form id="addForm" class="form-horizontal" action="${basepath}/manage/catalog/insertJson" method="post">
        <div class="row">
            <div class="control-group span16">
                <label class="control-label">目录名称：</label>

                <div class="controls">
                    <input type="text" class="input-normal control-text" name="name" data-rules="{required : true}">
                </div>
            </div>
            <div class="control-group span16">
                <label class="control-label">一级目录：</label>

                <div class="controls">
                    <div id="ridSelect">
                        <input type="hidden" id="rid" name="pid">
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
<#------------------------------------------------------添加表单end------------------------------------------------------->
<#------------------------------------------------------编辑表单start------------------------------------------------------->
<div id="editContent" style="display:none;">
    <form id="editForm" class="form-horizontal" action="${basepath}/manage/catalog/updateJson" method="post">
        <input type="hidden" class="input-normal control-text" name="id"/>

        <div class="row">
            <div class="control-group span16">
                <label class="control-label">目录名称：</label>
                <div class="controls">
                    <input type="text" class="input-normal control-text" name="name" data-rules="{required : true}">
                </div>
            </div>
            <div class="control-group span16">
                <label class="control-label">一级目录：</label>
                <div class="controls">
                    <div id="editSelect">
                        <input type="hidden" id="editRid" name="pid">
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
<#------------------------------------------------------编辑表单end------------------------------------------------------->
<script>
    /*----------------------------------------------列表页面 begin------------------------------------*/
    var Grid = BUI.Grid,
            Store = BUI.Data.Store,
            columns = [
                {title: '类别名称', dataIndex: 'name',  elCls: 'center'},
                {
                    title: '目录等级', dataIndex: 'pid', elCls: 'center', renderer: function (data) {
                    if (data == "0") {
                        return '一级目录';
                    }
                    else {
                        return '二级目录';
                    }
                }
                },
                {title: '是否在导航条显示', dataIndex: 'showInNav', elCls: 'center'},
                {
                    title: '操作', dataIndex: 'id', elCls: 'center', renderer: function (value) {
                    return '<a href="javascript:edit(' + value + ')">编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;<a  href="javascript:delFunction();">删除</a>';
                }
                }
            ];
    var store = new Store({
                url: 'loadData',
                autoLoad: true, //自动加载数据
                params: { //配置初始请求的参数
                    length: '10',
                    status: $("#status").val()
                },
                pageSize: 10,	// 配置分页数目
                root: 'list',
                totalProperty: 'total'
            }),
            grid = new Grid.Grid({
                render: '#t1',
                columns: columns,
                forceFit: true,
                loadMask: true, //加载数据时显示屏蔽层
                store: store,
                plugins: [Grid.Plugins.CheckSelection], // 插件形式引入多选表格
                // 底部工具栏
                bbar: {
                    pagingBar: true
                }
            });
    grid.render();
    var form = new BUI.Form.HForm({
        srcNode: '#searchForm'
    }).render();
    form.on('beforesubmit', function (ev) {
        //序列化成对象
        var obj = form.serializeToObject();
        obj.start = 0; //返回第一页
        store.load(obj);
        return false;
    });
    /*----------------------------------------------列表页面 end------------------------------------*/
    //删除选中的记录
    function delFunction() {
        BUI.Message.Confirm('删除后改商品目录下所有的商品将无法显示？<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;是否确认删除？', function () {
            var selections = grid.getSelection();
            var ids = new Array();
            for (var i = 0; i < selections.length; i++) {
                ids[i] = selections[i].id.toString()
            }
            $.ajax({
                type: "POST",
                url: "${basepath}/manage/catalog/deletesJson",
                dataType: "json",
                data: {
                    ids: ids
                },
                success: function (data) {
                    var obj = form.serializeToObject();
                    obj.start = 0; //返回第一页
                    store.load(obj);
                }
            });
        });
    }
    /*----------------------------------------------新增表单处理 begin------------------------------------*/
    var Select = BUI.Select, Data = BUI.Data;
    var addStore = new Store({
        url: '${basepath}/manage/catalog/getParentCatalog',
        autoLoad: true,
        root: 'data'
    });
    var roleSelect = new Select.Select({
        render: '#ridSelect',
        valueField: '#rid',
        list: {
            itemTpl: '<li>{name}</li>',
            idField: 'id',
            elCls: 'bui-select-list'
        },
        store: addStore
    });
    roleSelect.render();
    var addForm = new BUI.Form.Form({
        srcNode: '#addForm',
        submitType: 'ajax',
        callback: function (data) {
//            var obj = new Object();
//            obj.start = 0;
//            store.load(obj);
            addDialog.close();
        }
    }).render();

    var addDialog = new BUI.Overlay.Dialog({
        title: '添加目录',
        width: 400,
        height: 200,
        contentId: 'addContent',
        success: function () {
            if (addForm.isValid()) {
                addForm.ajaxSubmit();
                window.location.href="${basepath}/manage/catalog/selectProductCatalogList";

            } else {
                addForm.valid();
            }
        }
    });
    //添加按钮事件
    function add() {
        addForm.clearFields();
        addStore.load();
        addDialog.show();
    }
    /*----------------------------------------------新增表单处理 end------------------------------------*/
    /*----------------------------------------------编辑表单处理 begin------------------------------------*/

    var editStore = new Data.Store({
        url: '${basepath}/manage/catalog/getParentCatalog',
        autoLoad: true,
        root: 'data'
    });
    var editSelect = new Select.Select({
        render: '#editSelect',
        valueField: '#editRid',
        list: {
            itemTpl: '<li>{name}</li>',
            idField: 'id',
            elCls: 'bui-select-list'
        },
        store: editStore
    });
    editSelect.render();
    var editForm = new BUI.Form.Form({
        srcNode: '#editForm',
        submitType: 'ajax',
        callback: function (data) {
            var obj = new Object();
            obj.start = 0; //返回第一页
            store.load(obj);
            editDialog.close();
        }
    });
    editForm.render();
    var editDialog = new BUI.Overlay.Dialog({
        title: '编辑目录',
        width: 400,
        height: 200,
        contentId: 'editContent',
        success: function () {
            editForm.ajaxSubmit();
        }
    });
    //编辑按钮事件
    function edit(id) {
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "${basepath}/manage/catalog/toEditJson",//弹窗编辑
            data: {id: id},
            success: function (data) {
                var form = $("#editForm")[0];
                BUI.FormHelper.setFields(form, data.data);
                editStore.load();
                editDialog.show();
            }
        });
    }
    /*----------------------------------------------编辑表单处理 end------------------------------------*/
</script>
</@page.pageBase>